package com.restart.game.common.util.img;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @author ReStartLin
 * @data 2018/12/26 17:01
 * @classDesc: 功能描述:
 */
public class ImgMosaic {

    /**
     * 马赛克  97973
     * @param srcImageFile
     * @param imageFormat
     * @return
     * @throws IOException
     */
    public static File mosaic(File srcImageFile,String imageFormat) throws IOException {
        FileOutputStream fos = null;
        try {
        //获取源图片
            BufferedImage image = ImageIO.read(srcImageFile);
            //图片的宽
            int imgWidth = image.getWidth();
            //图片的高
            int imgHeight = image.getHeight();
            //马赛克位置
            int x = imgWidth - 138;
            int y = imgHeight - 38;
            //马赛克大小
            int width = 130;
            int height = 33;
            int xx = x;
            //根据xy点坐标绘制连接线
            Graphics2D g2d = image.createGraphics();
        //填充一个矩形
            int xcount = 0;
            int ycount = 0;
            if(width%4==0){
                xcount = width/4;//计算需要横向填充多少个矩形
            }else{
                xcount = width/4 + 1;//如果不被整除+1
            }
            if(height%4==0){
                ycount = height/4;//计算需要纵向填充多少个矩形
            }else{
                ycount = height/4 + 1;//如果不被整除+1
            }
            for(int j = 0;j<ycount;j++){
                for(int i = 0;i<xcount;i++){
                    int mwidth = 4;
                    int mheight = 4;
                    if(i==xcount-1){ //横向最后一个比较特殊，可能不够一个size
                        mwidth = width-x;
                    }
                    if(j == ycount-1){ //同理
                        mheight =height-y;
                    }
                    int centerX = x;
                    int centerY = y;
                    if (mwidth % 2 == 0) {
                        centerX += mwidth / 2; //计算横向中心点
                    } else {
                        centerX += (mwidth - 1) / 2;
                    }
                    if (mheight % 2 == 0) {
                        centerY += mheight / 2; //计算纵向向中心点
                    } else {
                        centerY += (mheight - 1) / 2;
                    }
                    Color color = new Color(image.getRGB(centerX, centerY)); //获取中心点数据
                    g2d.setColor(color); //稀释
                    g2d.fillRect(x, y, 4, 4);
                    x = x+4;
                }
                y=y+4;
                x = xx;
            }
            g2d.dispose();
            File imgFile = File.createTempFile(ImgOSS.imageName(), "."+imageFormat);
            fos = new FileOutputStream(imgFile);
            ImageIO.write(image, imageFormat, fos);
            return imgFile;
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            if(fos!=null){
                fos.close();
            }
        }
        return null;
    }
}
